let usernameInput = $('input[name="username"]')
let passwordInput = $('input[name="password"]')
let userLogin = []
$('button[name="login"]').on('click', function () {
    let username = usernameInput.val()
    let password = passwordInput.val()
    // 正则表达式
    let reg = /\S/
    if (reg.test(username) == false || reg.test(password) == false) {
        alert("用户名或密码不能为空")
        return
    }
    let userStr = {
        username,
        password
    }
    let data = JSON.parse(localStorage.getItem('userList')) || []
    if (data.length == 0) {
        alert("该用户不存在,请注册")
        return
    }
    let user = data.find(item => item.username == userStr.username)
    if (user.username != userStr.username) {
        alert("该用户不存在,请注册")
        return
    } else {
        let newData = data.find((item) => item.username == userStr.username && item.password == userStr.password)
        if (newData) {
            alert("登录成功")
            userLogin.push(userStr)
            localStorage.setItem('userLogin', JSON.stringify({
                username
            }))
            location.href = './index.html'
            return
        } else {
            alert("用户名或密码错误")
            return
        }
    }

})